package xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.thoughtworks.xstream.XStream;

import dto.xml.XMLArticuloHogarDTO;
import dto.xml.XMLArticuloRopaDTO;
import dto.xml.XMLDetallesHogarDTO;
import dto.xml.XMLDetallesRopaDTO;
import dto.xml.XMLItemPedido2DTO;
import dto.xml.XMLItemPedidoDTO;
import dto.xml.XMLOfadArticuloHogarDTO;
import dto.xml.XMLOfadArticuloRopaDTO;
import dto.xml.XMLOfadDTO;
import dto.xml.XMLOfadHogarDTO;
import dto.xml.XMLOfadRopaDTO;
import dto.xml.XMLPalcDTO;
import dto.xml.XMLPedido2DTO;
import dto.xml.XMLPedidoDTO;
import dto.xml.XMLSolDistDTO;

public class XMLParser {
	private static PropertiesConfiguration configuration;
	private static XStream xstream = new XStream();
	
    public XMLParser() {
//		super();
    	//System.out.println("Instancio el Parser");
		try {
			configuration = new PropertiesConfiguration("configEJB.properties");
			
			//SolDist
			xstream.alias("articulos", XMLItemPedidoDTO.class);
			//xstream.alias("PEPE", XMLItemPedidoDTO.class);
			xstream.alias("solDist", XMLSolDistDTO.class);
			xstream.alias("envio", XMLPedidoDTO.class);
			xstream.addImplicitCollection(XMLPedidoDTO.class, "itemsPedidos");
			xstream.addImplicitCollection(XMLSolDistDTO.class, "pedidos");
			
			//Articulos Ropa y Hogar
			xstream.alias("nuevoArt", XMLArticuloHogarDTO.class);
			xstream.alias("nuevoArt", XMLArticuloRopaDTO.class);
			xstream.alias("accesorio", XMLDetallesHogarDTO.class);
			xstream.alias("ropa", XMLDetallesRopaDTO.class);
			
			//Palc
			xstream.alias("palc", XMLPalcDTO.class);
			xstream.alias("pedidos", XMLPedido2DTO.class);
			xstream.alias("pedido", XMLItemPedido2DTO.class);
			xstream.addImplicitCollection(XMLPedido2DTO.class, "itemsPedidos");
			
			//Ofad
			xstream.alias("ofad", XMLOfadDTO.class);
			xstream.alias("ropa", XMLOfadRopaDTO.class);
			xstream.aliasField("accesorios-hogar", XMLOfadDTO.class, "accesorios_hogar");
			xstream.alias("articulo", XMLOfadArticuloRopaDTO.class);
			xstream.alias("accesorio", XMLOfadArticuloHogarDTO.class);
			xstream.addImplicitCollection(XMLOfadHogarDTO.class, "articulosHogar");
			xstream.addImplicitCollection(XMLOfadRopaDTO.class, "articulosRopa");
			
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
    
    public static String getXmlString(Object o) throws IOException{
    	String path = configuration.getString("xml.path");
    	String output=null;
    	SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd-hhss");
    	String dia = date.format(new Date());
    	
    	String xmlName = o.getClass().getSimpleName();
    	
    	String[] splitter = xmlName.split("XML");
    	String string2 = splitter[1];
    	splitter = string2.split("DTO");
    	xmlName = splitter[0];
    	
    	String completePath = path+xmlName+"-"+dia+".xml";
    	//String nombreAguardar= "/home/" + xmlName+"-"+dia+".xml";
   		try {
   			//xstream.toXML(o, new FileOutputStream(completePath));
   			FileOutputStream outputStream = new FileOutputStream(completePath);
   		    Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
   		    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
   		    xstream.toXML(o,writer);
   		    output= xstream.toXML(o);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
   		
   		return output;  	
    }
    
	public static String generateXmlAndSave(Object o) throws IOException  {
    	String path = configuration.getString("xml.path");
    	SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd-hhss");
    	String dia = date.format(new Date());
    	
    	String xmlName = o.getClass().getSimpleName();
    	
    	String[] splitter = xmlName.split("XML");
    	String string2 = splitter[1];
    	splitter = string2.split("DTO");
    	xmlName = splitter[0];
    	
    	String completePath = path+xmlName+"-"+dia+".xml";
    	//String nombreAguardar= "/home/" + xmlName+"-"+dia+".xml";
   		try {
   			//xstream.toXML(o, new FileOutputStream(completePath));
   			FileOutputStream outputStream = new FileOutputStream(completePath);
   		    Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
   		    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
   		    xstream.toXML(o,writer);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
   		
   		return completePath;
    }
    
    public static Object parseToObject(File f) throws FileNotFoundException{
        return xstream.fromXML(new FileInputStream(f));
    }

    public static Object parseToObject(String f){
        return xstream.fromXML(f);
	}
    
}
